#include "stdafx.h"

#include <iostream>

#include "../menu/Report.h"
#include "../db/db_connection.h"
#include "../db_generator/generated.h"

using namespace std;
using namespace uhdpbs::db;
using namespace uhdpbs::menu;
using namespace uhdpbs::generated;
using namespace uhdpbs::reports;



void ShowPatientList(connection& dbConn) {
	patient_table tbl = patient_table(&dbConn);
	tbl.load();
	generated::patient_list(&tbl);
}
void ShowPractitionerList(connection& dbConn) {
	practitioner_table tbl = practitioner_table(&dbConn);
	tbl.load();
	generated::practitioner_list(&tbl);
}
void ShowPaymentList(connection& dbConn) {
	payment_table tbl = payment_table(&dbConn);
	tbl.load();
	generated::payment_list(&tbl);
}
void ShowVisitList(connection& dbConn) {
	visit_table tbl = visit_table(&dbConn);
	tbl.load();
	generated::visit_list(&tbl);
}
void ShowAppointmentList(connection& dbConn) {
	appointment_table tbl = appointment_table(&dbConn);
	tbl.load();
	generated::appointment_list(&tbl);
}
void AcceptPayment(connection& dbConn) {
	payment_table tbl = payment_table(&dbConn);
	tbl.load();

	payment_row newRow = tbl.create_new_row();
	generated::edit_row(&newRow);

	tbl.get_rows()->push_back(newRow);
	tbl.save();
}
void ScheduleAppointment(connection& dbConn) {
	appointment_table tbl = appointment_table(&dbConn);
	tbl.load();

	appointment_row newRow = tbl.create_new_row();
	generated::edit_row(&newRow);

	tbl.get_rows()->push_back(newRow);
	tbl.save();
}


void AgedReceivables(connection& dbConn) {
	Receivables rpt = Receivables();
	rpt.AgedReceivablesMenu(dbConn);
}

void MasterListsMenu(connection& dbConn) {
	int iStartPosition = 1;
	while(true) {
		Menu mnu = Menu("Master List", " > ", "");

		mnu.AddOption("Appointments");
		mnu.AddOption("Patients");
		mnu.AddOption("Payments");
		mnu.AddOption("Practitioner");
		mnu.AddOption("Visits");

		mnu.AddOption("Exit to Main Menu");

		int iSelection = mnu.Play(iStartPosition);
		VP_ClearScreen();
		switch(iSelection) {
			case 1: ShowAppointmentList(dbConn); break;
			case 2: ShowPatientList(dbConn); break;
			case 3: ShowPaymentList(dbConn); break;
			case 4: ShowPractitionerList(dbConn); break;
			case 5: ShowVisitList(dbConn); break;
			case 6: 
				return;
			default: 
				cout << "Invalid selection" << endl;
				cout << "Press enter to continue...";
				cin.get();
				break;
		}

		iStartPosition = iSelection;
	}
}

void BillingMenu(connection& dbConn) {
	int iStartPosition = 1;
	while(true) {
		Menu mnu = Menu("Billing", " > ", "");

		mnu.AddOption("Add a Visit");
		mnu.AddOption("Add a Payment");
		
		mnu.AddOption("Exit to Main Menu");

		int iSelection = mnu.Play(iStartPosition);
		VP_ClearScreen();
		switch(iSelection) {
			case 1: ScheduleAppointment(dbConn); break;
			case 2: AcceptPayment(dbConn); break;
			case 3: 
				return;
			default: 
				cout << "Invalid selection" << endl;
				cout << "Press enter to continue...";
				cin.get();
				break;
		}

		iStartPosition = iSelection;
	}
}

void ReportingMenu(connection& dbConn) {
	int iStartPosition = 1;
	while(true) {
		Menu mnu = Menu("Reporting List", " > ", "");

		mnu.AddOption("Aged Receivables");
	
		mnu.AddOption("Exit to Main Menu");

		int iSelection = mnu.Play(iStartPosition);
		VP_ClearScreen();
		switch(iSelection) {
			case 1: AgedReceivables(dbConn); break;
			case 2: 
				return;
			default: 
				cout << "Invalid selection" << endl;
				cout << "Press enter to continue...";
				cin.get();
				break;
		}

		iStartPosition = iSelection;
	}
}



void MainMenu(connection& dbConn) {
	int iStartPosition = 1;
	while(true) {
		Menu mnu = Menu("UHD Patient Billing System", " > ", "");

		mnu.AddOption("Master Lists");
		mnu.AddOption("Schedule Visit");
		mnu.AddOption("Bill Patient");
		mnu.AddOption("Accept Payment");
		mnu.AddOption("Statements");

		mnu.AddOption("Exit Program");

		int iSelection = mnu.Play(iStartPosition);
		VP_ClearScreen();
		switch(iSelection) {
			case 1: MasterListsMenu(dbConn); break;
			case 2: ScheduleAppointment(dbConn); break;
			case 3: BillingMenu(dbConn); break;
			case 4: AcceptPayment(dbConn); break;
			case 5: ReportingMenu(dbConn);break;
			case 6: 
				return;
			default: 
				cout << "Invalid selection" << endl;
				cout << "Press enter to continue...";
				cin.get();
				break;
		}

		iStartPosition = iSelection;
	}
}

int _tmain(int argc, _TCHAR* argv[]) {
	cout << "Patient Billing System Project" << endl;
	cout << "CS3420 Software Engineering (CRN 10616)" << endl;
	cout << "University of Houston Downtown" << endl;
	cout << endl;
	cout << "Contributors: Danny Ho, Latoyya Smith, Daniel Sanders, and Shawn Zernik" << endl;
	cout << "(C) Copyright 2012" << endl;
	cout << endl;
    cout << "This program is free software: you can redistribute it and/or modify" << endl;
    cout << "it under the terms of the GNU General Public License as published by" << endl;
    cout << "the Free Software Foundation, either version 3 of the License, or" << endl;
    cout << "(at your option) any later version." << endl;
	cout << endl;
    cout << "This program is distributed in the hope that it will be useful," << endl;
    cout << "but WITHOUT ANY WARRANTY; without even the implied warranty of" << endl;
    cout << "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the" << endl;
    cout << "GNU General Public License for more details." << endl;
	cout << endl;
    cout << "You should have received a copy of the GNU General Public License" << endl;
    cout << "along with this program.  If not, see <http://www.gnu.org/licenses/>.	" << endl;
	cout << endl;
	cout << "Press enter to continue...";
	cin.get();
	VP_ClearScreen();

	cout << "Server: ";
	string sServer = "";
	cin >> sServer;
	//cout << "Database: ";
	string sDatabase = "uhdpbs_db";
	//cin >> sDatabase;
	cout << "Username: ";
	string sUser = "";
	cin >> sUser;
	cout << "Password: ";
	string sPassword = "";
	cin >> sPassword;

	connection dbConn = connection();
	if(!dbConn.open(sServer, sDatabase, sUser, sPassword)) {
		cout << "Could not connect to the database server!" << endl;
		cout << dbConn.get_error() << endl;
		cout << "Press enter to continue..." << endl;
		cin.get();
		return -1;
	}

	MainMenu(dbConn);

	return 0;
}